from openpyxl import Workbook
from openpyxl.utils import get_column_letter
import requests
from bs4 import BeautifulSoup

# 示例HTML字符串
html_string = '<table><img src="https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1oC6Pn.img" height=50 width=50></table>'


# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_string, 'html.parser')
img_tag = soup.find('img')

if img_tag and 'src' in img_tag.attrs:
    img_url = img_tag['src']

    # 创建工作簿和工作表
    wb = Workbook()
    ws = wb.active
    ws.title = "Images"

    # 插入图片URL到单元格A1
    ws['A1'] = img_url

    # 使用HYPERLINK公式添加图片链接
    # ws['B1'] = f'=HYPERLINK("{img_url}", "Image Link")'
    ws['B1'] = f'<table><img src="https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1oC6Pn.img" height=50 width=50></table>'

    # 保存工作簿
    wb.save("ImageURLInExcel.xlsx")

else:
    print("No valid <img> tag found in HTML.")
